﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace GroupShop.DAL
{
    public sealed class DBConnHelper
    {
        private static DBConnHelper instance;
        private static readonly object obj = new object();       

        private DBConnHelper()
        { }

        public static DBConnHelper Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (obj)
                    {
                        if (instance == null)
                        {
                            instance = new DBConnHelper();
                        }
                    }
                }

                return instance;
            }
        }

        public string GetSQLConnectionString()
        {
            return ConfigurationManager.AppSettings["SQLConnection"].ToString();
        }

        /// <summary>
        /// Opens a  sql connection.
        /// </summary>
        /// <param name="connectionString">The connection string.</param>
        /// <returns>Returns a opening sqlconnection object. </returns>
        public SqlConnection OpenSqlConnection(string connectionString)
        {
            SqlConnection conn = new SqlConnection(connectionString);
            conn.Open();
            return conn;
        }

        public void CloseSqlConnection(SqlConnection conn)
        {
            if (conn == null)
            {
                throw new ArgumentNullException("The sql connection is null!");
            }

            if (conn.State == ConnectionState.Open)
            {
                conn.Close();
            }
        }

    }
}
